var express = require('express');
var Signing = require('../modules/signing')
var router = express.Router();

// 添加
router.post('/', function (req, res) {
  var { people, peopleNo, user, userNo, address, day, type, work, other, date, time, wage, rest, money, standard, buy } = req.body
  console.log(req)
  var signing = new Signing({ people, peopleNo, user, userNo, address, day, type, work, other, date, time, wage, rest, money, standard, buy })
  signing.save(function (err) {
    if (err) {
      res.json({
        success: false,
        message: "录入失败！"
      })
    }
  })
  res.json({ success: true, message: "录入成功！" })
})

// 查看
router.get('/', function (req, res) {
  Signing.find({}, function (err, categories) {
    res.json({
      success: true,
      data: categories
    })
  })
})

// 更新
router.put('/', function (req, res) {

  // 解构赋值
  var { title, newTitle } = req.body;
  Signing.findOneAndUpdate({ title: title }, { title: newTitle }, function (err, signing) {
    if (err) {
      res.json({
        success: false, message: "更新失败"
      })
    }
  })
  res.json({ success: true, message: "更新成功！" })
})

// 删除
router.delete('/', function (req, res) {
  console.log('delete signing');

  // 解构赋值
  var { title } = req.body;

  Signing.remove({ title: title }, function (err) {
    if (err) {
      res.json({
        success: false, messge: "删除失败！"
      })
    }
  })
  res.json({ success: true, message: "删除成功！" })
})

module.exports = router;